home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 23 / AACD 23.iso / AACD / Programming / tek / kn / amiga / sock.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-05-12  |  3.3 KB  |  138 lines

  1.  
  2. /*
  3. **    TEKlib
  4. **    (C) 2001 TEK neoscientists
  5. **    all rights reserved.
  6. **
  7. **    tek/kn/amiga/sock.c
  8. **    AmigaOS 3.x bsdsocket.library backend
  9. **
  10. **    TODO:
  11. **        amiga WaitSelect() isn't interrupted by timeout ->
  12. **        no timeout handling when no traffic!
  13. **
  14. */
  15.  
  16. #include <exec/types.h>
  17. #include <proto/dos.h>
  18. #include <proto/exec.h>
  19.  
  20. #include <proto/socket.h>
  21. #include <netinet/in.h>
  22. #include <amitcp/socketbasetags.h>
  23. #include <sys/types.h>
  24. #include <sys/socket.h>
  25. #include <sys/time.h>
  26. #include <sys/errno.h>
  27. #include <sys/ioctl.h>
  28. #include <netdb.h>
  29. #include <string.h>
  30.  
  31.  
  32. #include "tek/kn/sock.h"
  33. #include "tek/kn/amiga/exec.h"
  34.  
  35.  
  36.  
  37. #define KNSOCK_MAXLISTEN        16            /* max number of connections to a server socket */
  38. #define KNSOCK_MAXPENDING        32            /* max number of concurrent messages on a client socket pending for delivery */
  39. #define TIMEOUT_USEC            10000        /* select() timeout */
  40. #define KNSOCK_SENDFLAGS        0
  41. #define KNSOCK_RECVFLAGS        0
  42.  
  43.  
  44. typedef struct
  45. {
  46.     TAPTR socketbase;
  47.     LONG *sockerrno;
  48.  
  49. } kn_sockenv_t;
  50.  
  51.  
  52. #include "tek/kn/sockcommon.h"
  53.  
  54.  
  55.  
  56. #define SysBase *((struct ExecBase **) 4L)
  57.  
  58. static TBOOL kn_getsockenv(kn_sockenv_t *sockenv)
  59. {
  60.     struct amithread *self = (FindTask(NULL))->tc_UserData;
  61.     if (self)
  62.     {
  63.         if (!self->socketbase)
  64.         {
  65.             self->socketbase = OpenLibrary("bsdsocket.library", 4);
  66.             if (self->socketbase)
  67.             {
  68.                 #define SocketBase self->socketbase
  69.                 SetErrnoPtr(&self->sockerrno, sizeof(LONG));
  70.                 #undef SocketBase
  71.             }
  72.         }
  73.         
  74.         if (self->socketbase)
  75.         {
  76.             sockenv->socketbase = self->socketbase;
  77.             sockenv->sockerrno = &self->sockerrno;
  78.             return TTRUE;
  79.         }
  80.         else
  81.         {
  82.             dbsprintf(20,"*** TEKLIB getsockenv: could not open bsdsocket.library v4\n");
  83.         }
  84.     }
  85.     else
  86.     {
  87.         dbsprintf(40,"*** TEKLIB getsockenv ALERT: could not find self\n");
  88.     }
  89.  
  90.     return TFALSE;
  91. }
  92.  
  93. #undef SysBase
  94.  
  95.  
  96. #define SocketBase    ((kn_sockenv_t *) sockenv)->socketbase
  97.  
  98.  
  99. int kn_waitselect(kn_sockenv_t *sockenv, int n, fd_set *r, fd_set *w, fd_set *e, struct timeval *t, TKNOB *evt, TBOOL *signal)
  100.     ULONG amisig = 1L << ((struct amievent *) evt)->signal;
  101.     int numready = WaitSelect(n, r, w, e, TNULL, &amisig);
  102.     *signal = amisig;
  103.     return numready;
  104. }
  105.  
  106. #define kn_getsockerrno(sockenv,desc)    *(((kn_sockenv_t *) sockenv)->sockerrno)
  107. #define kn_locksock(sockenv)
  108. #define kn_unlocksock(sockenv)
  109. #define kn_inet_ntoa(name)                Inet_NtoA(((struct sockaddr_in *) (name))->sin_addr.s_addr)
  110. #define kn_setsockopts(desc)            { LONG yes = 1; setsockopt(desc, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); }
  111. #define kn_closesocket(desc)            CloseSocket(desc)
  112. #define kn_socknonblocking(desc)        { LONG yes = 1; IoctlSocket(desc, FIONBIO, (char *) &yes); }
  113.  
  114. #include "tek/kn/sockcommon/initsockname.c"
  115. #include "tek/kn/sockcommon/destroysockname.c"
  116. #include "tek/kn/sockcommon/cmpsockname.c"
  117. #include "tek/kn/sockcommon/dupsockname.c"
  118. #include "tek/kn/sockcommon/getsockname.c"
  119. #include "tek/kn/sockcommon/getsockport.c"
  120.  
  121. #include "tek/kn/sockcommon/createclientsock.c"
  122. #include "tek/kn/sockcommon/destroyclientsock.c"
  123. #include "tek/kn/sockcommon/getclientsockmsg.c"
  124. #include "tek/kn/sockcommon/putclientsockmsg.c"
  125. #include "tek/kn/sockcommon/waitclientsock.c"
  126.  
  127. #include "tek/kn/sockcommon/createservsock.c"
  128. #include "tek/kn/sockcommon/destroyservsock.c"
  129. #include "tek/kn/sockcommon/getservsockmsg.c"
  130. #include "tek/kn/sockcommon/returnservsockmsg.c"
  131. #include "tek/kn/sockcommon/waitservsock.c"
  132.  
  133. #include "tek/kn/sockcommon/itoa.c"
  134.  
  135.  
  136. #undef SocketBase
  137.